﻿using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TookKit.Mvvm.Attributes;

namespace LoveNote.Database.Sqlite.Repositories
{
    [DependencyInjection(TookKit.Mvvm.Enums.Lifetime.Singleton, typeof(INoteRepository))]
    class NoteRepository : RepositoryBase, INoteRepository
    {
        public int Delete(NoteEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            return db.SaveChanges();
        }

        public List<NoteEntity> GetAll()
        {
            return db.Notes.ToList();
        }

        public int Insert(NoteEntity entity)
        {
            db.Notes.Add(entity);
            return db.SaveChanges();
        }

        public int SaveChanged()
        {
            return db.SaveChanges();
        }

        public List<NoteEntity> Search(string keyword)
        {
            ArgumentNullException.ThrowIfNull(keyword, nameof(keyword));
            return db.Notes.Where(t => t.Content.Contains(keyword)).ToList();
        }

        public int Update(NoteEntity entity)
        {
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
